import { UserConfigExport, ConfigEnv } from 'vite'
import { resolve } from 'path'
import vue from '@vitejs/plugin-vue'
import { configSvgIconsPlugin } from './src/plugins/configSvgIconsPlugin'
import vueJsx from '@vitejs/plugin-vue-jsx'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vitejs.dev/config/
export default ({ command }: ConfigEnv): UserConfigExport => {
    const root = process.cwd()
    const isBuild = command === 'build'
    return {
        root,
        plugins: [
            vue(),
            vueJsx(),
            configSvgIconsPlugin(isBuild, [
                'src/icons/svg/base',
                'src/icons/svg/resource',
                'src/icons/svg/security'
            ]),
            Components({
                resolvers: [ElementPlusResolver()]
            })
        ],
        resolve: {
            alias: [
                { find: '@', replacement: resolve(process.cwd(), 'src') },
                { find: '#', replacement: resolve(process.cwd(), 'types') }
            ]
        },
        server: {
            port: 9528,
            open: false,
            proxy: {
                '/api': {
                    target: 'http://localhost:8010',
                    changeOrigin: true,
                    ws: true,
                    rewrite: (path) => path.replace(new RegExp('^/api'), '')
                }
            }
        },
        build: {
            // sourcemap: true,
            manifest: true,
            rollupOptions: {
                output: {
                    manualChunks: {
                        vue: ['vue', 'vue-router', 'vuex'],
                        'element-plus': ['element-plus']
                    }
                }
            },
            chunkSizeWarningLimit: 500
        }
    }
}
